package com.burst.reptile.modular.zp.service.impl;

import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.burst.reptile.common.exception.CustomException;
import com.burst.reptile.common.utils.SecurityUtils;
import com.burst.reptile.common.utils.ToolUtil;
import com.burst.reptile.framework.security.service.TokenService;
import com.burst.reptile.modular.api.form.AnswerForm;
import com.burst.reptile.modular.zp.entity.ZpQuestion;
import com.burst.reptile.modular.zp.mapper.ZpQuestionMapper;
import com.burst.reptile.modular.zp.service.IZpQuestionService;

import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;

/**
 * <p>
 * 问问列表 服务实现类
 * </p>
 *
 * @author wujin
 * @since 2020-06-29
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ZpQuestionServiceImpl extends ServiceImpl<ZpQuestionMapper, ZpQuestion> implements IZpQuestionService {

	private final TokenService tokenService;
	
	@Override
	public int add(ZpQuestion question) {
		question.setCity(tokenService.getCitySite());
		return this.baseMapper.insert(question);
	}

	@Override
	public Page<ZpQuestion> pageList(Page<ZpQuestion> page, String keyWord, String questionType) {
		QueryWrapper<ZpQuestion> queryWrapper = new QueryWrapper<>();
		if(StrUtil.isNotEmpty(keyWord)) {
			queryWrapper.like("question", keyWord).or().like("answer", keyWord);
		}
		if(StrUtil.isNotEmpty(questionType)) {
			queryWrapper.eq("question_type", questionType);
		}
		queryWrapper.eq("city", tokenService.getCitySite());
		queryWrapper.eq("qu_status", "1");
		queryWrapper.orderByDesc("create_time");
		return this.page(page, queryWrapper);
	}

	@Override
	public Page<ZpQuestion> pageList(Page<ZpQuestion> page, ZpQuestion question) {
		QueryWrapper<ZpQuestion> queryWrapper = new QueryWrapper<>();
		String phone = question.getPhone();
		if(StrUtil.isNotEmpty(phone)) {
			queryWrapper.eq("phone", phone);
		}
		String userName = question.getUserName();
		if(StrUtil.isNotEmpty(userName)) {
			queryWrapper.like("user_name", userName);
		}
		queryWrapper.isNull("consultant"); // 没有指定咨询师
		queryWrapper.eq("qu_status", "0"); // 待解答状态
		queryWrapper.orderByDesc("create_time");
		return this.page(page, queryWrapper);
	}

	@Override
	public int answer(AnswerForm answerForm) {
		Integer id = answerForm.getId();
		ZpQuestion byId = this.getById(id);
		if(ToolUtil.isEmpty(byId)) {
			throw new CustomException("回答的资源不存在");
		}
		byId.setAnswer(answerForm.getAnswer());
		byId.setQuestionType(answerForm.getQuestionType());
		byId.setAnswerTime(LocalDateTime.now());
		byId.setQuStatus("1");
		byId.setConsultant(SecurityUtils.getLoginUserId());
		return this.baseMapper.updateById(byId);
	}

	@Override
	public Page<ZpQuestion> pageAnswerList(Page<ZpQuestion> page, ZpQuestion question) {
		QueryWrapper<ZpQuestion> queryWrapper = new QueryWrapper<>();
		String phone = question.getPhone();
		if(StrUtil.isNotEmpty(phone)) {
			queryWrapper.eq("phone", phone);
		}
		String userName = question.getUserName();
		if(StrUtil.isNotEmpty(userName)) {
			queryWrapper.like("user_name", userName);
		}
		queryWrapper.eq("consultant", SecurityUtils.getLoginUserId()); // 没有指定咨询师
		queryWrapper.orderByDesc("create_time");
		return this.page(page, queryWrapper);
	}

}
